/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.LoginResponse;
import io.renren.modules.app.entity.Response;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.app.service.UserService;
import io.renren.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     */

    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@RequestParam String openId) {
        UserEntity user = userService.queryByOpenid(openId);
        LoginResponse response = new LoginResponse();
        if (user == null) {
            // 处理用户不存在的情况
            response.setCode(404);
            response.setMessage("用户未注册");
            response.setIsLogin(false);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
        // 用户存在，返回用户信息
        response.setCode(0);
        response.setMessage("登录成功");
        response.setData(user);
        response.setIsLogin(true);
        return ResponseEntity.status(HttpStatus.OK).body(response);
    }


}
